package com.harry.hr.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.harry.hr.dao.RoleDAO;
import com.harry.hr.entity.system.Role;
import com.harry.hr.entity.system.User;

public class RoleDAOImpl extends HibernateDaoSupport implements RoleDAO {

	public void clear(Role role) {
		this.getHibernateTemplate().delete(role);

	}

	public void delete(Role role) {
		role = (Role)this.getHibernateTemplate().get(Role.class, role.getRoleId());
		role.setRoleStatus(0);
		this.getHibernateTemplate().update(role);
	}

	@SuppressWarnings("unchecked")
	public List<Role> query(User user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		if(user!=null){
			if(user.getId()>0)
				criteria.createCriteria("users").add(Restrictions.idEq(user.getId()));
			criteria.add(Restrictions.eq("roleStatus", 1));
		}
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	public void save(Role role) {
		this.getHibernateTemplate().save(role);

	}

	public void update(Role role) {
		this.getHibernateTemplate().update(role);

	}

}
